<template>
  <div class="app-container">
    <div class="filter-container" align="left">
      <el-input class="filter-item" placeholder="请输入类型" style="width: 200px;" v-model="search" />
      <el-button @click="searchByName_" class="filter-item" type="primary" icon="el-icon-search" style="margin: 0 5px;">
        搜索
      </el-button>
      <el-button class="filter-item" icon="el-icon-edit" type="success" @click="handleCreate"
        style="margin-left: 10px;">新增商品类别</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="productTypeList" border style="width: 90%;">
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类别" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.producttypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini" plain type="danger" @click="handleDelete(scope.row.producttypeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹框 -->
    <el-dialog title="商品类别信息" :visible.sync="dialogFormVisible">
      <el-form :model="form" align="left">
        <el-form-item label="商品类型" :label-width="formLabelWidth">
          <el-select v-model="form.productcategoryId" placeholder="请选择商品类型">
            <el-option v-for="item in categoryOptions" :key="item.producCategoryId" :label="item.producttypeName"
              :value="item.productTypeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类别" :label-width="formLabelWidth">
          <el-input v-model="form.producttypeName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { selectAll, selectAllCategory, update, del, add, searchByName } from '../../api/productmanage/producttype.js'
export default {
  data() {
    return {
      search: '',
      dialogFormVisible: false,
      dialogStatus: '',
      updateId: null,
      formLabelWidth: '100px',
      productTypeList: [
        {
          producttypeId: 1,
          producttypeName: '宠物日常用品',
          type: '狗狗沐浴露',
        }
      ],
      // form: {
      //   productTypeId: '',
      //   producttypeName: '',
      //   productcategoryId: null,
      // },
      form: {},
      categoryOptions: [{
        productTypeId: '选项1', producttypeName: '宠物食品'
      }],
    }
  },
  methods: {
    handleCreate() {
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    // 新增
    async createData() {
      console.log(this.form)
      let res = await add(this.form);
      this.$notify({
        title: 'Success',
        message: 'Created Successfully',
        type: 'success',
        duration: 2000
      })
      this.dialogFormVisible = false;
      this.selectAll_();
    },
    handleUpdate(row) {
      this.form = row;
      this.dialogStatus = 'update';
      this.dialogFormVisible = true;
    },
    // 更新
    async updateData() {
      let res = await update(this.form);
      console.log(res)
      this.selectAll_();
      this.dialogFormVisible = false;
      this.$notify({
        title: 'Success',
        message: 'Update Successfully',
        type: 'success',
        duration: 2000
      })
    },// 删除
    async handleDelete(id) {
      let res = await del(id);
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.selectAll_();
    },
    // 查询所有
    async selectAll_() {
      let res = await selectAll();
      console.log(res)
      this.productTypeList = res.data;
    },
    // 查询所有商品类型 生成下拉框
    async selectAllCategory_() {
      let res = await selectAllCategory();
      this.categoryOptions = res.data;
    },
    // 根据种类名查询
    async searchByName_() {
      let res = await searchByName(this.search);
      console.log(res)
      this.productTypeList = res
    },
  },
  created() {
    this.selectAll_();
    this.selectAllCategory_();
  },
}
</script>

<style></style>